<?php

define('CRAWLER_BANKER_ACTION_MAPS', [
    'incr' => '抬价',
    'sold' => '出货',
    'decr' => '压价',
    'buy' => '进货',
    'test' => '测试',
    'burst_incr' => '爆涨杆',
    'burst_decr' => '爆跌杆',
    'unknown' => '看不透',
]);

function get_crawler_abnormal_time_line_infos($start_time = null, $end_time = null)
{/*{{{*/

    $str = '';
    if ($start_time) {
        $str .= ' and volume_at >= "'.$start_time.'"';
    }

    if ($end_time) {
        $str .= ' and volume_at <= "'.$end_time.'"';
    }

    return db_query('
        select
            left(volume_at, 10) as abnormal_date,
            count(id) as abnormal_count,
            sum(volume) as abnormal_sum
        from crawler_coin_abnormal_volume
        where 1 = 1'.$str.' 
        group by left(volume_at, 10) order by volume_at asc
    ');
}/*}}}*/

function get_crawler_abnormal_sum_key_distributed_infos($start_time = null, $end_time = null)
{/*{{{*/

    $str = '';
    if ($start_time) {
        $str .= ' and volume_at >= "'.$start_time.'"';
    }

    if ($end_time) {
        $str .= ' and volume_at <= "'.$end_time.'"';
    }

    return db_query('
    select t.key, t.abnormal_sum
    from
    (
        select `key`, sum(volume) as abnormal_sum from crawler_coin_abnormal_volume 
        where 1 = 1'.$str.' 
        group by `key`
    ) t order by t.abnormal_sum desc limit 12
    ');
}/*}}}*/

function get_crawler_abnormal_count_key_distributed_infos($start_time = null, $end_time = null)
{/*{{{*/
    $str = '';
    if ($start_time) {
        $str .= ' and volume_at >= "'.$start_time.'"';
    }

    if ($end_time) {
        $str .= ' and volume_at <= "'.$end_time.'"';
    }

    return db_query('
    select t.key, t.abnormal_count
    from
    (
        select `key`, count(id) as abnormal_count from crawler_coin_abnormal_volume 
        where 1 = 1'.$str.' 
        group by `key`
    ) t order by t.abnormal_count desc limit 12
    ');
}/*}}}*/

function get_crawler_abnormal_count_time_distributed_infos($start_time = null, $end_time = null)
{/*{{{*/
    $str = '';
    if ($start_time) {
        $str .= ' and volume_at >= "'.$start_time.'"';
    }

    if ($end_time) {
        $str .= ' and volume_at <= "'.$end_time.'"';
    }

    return db_query('
        select left(volume_at, 13) as `time`, count(id) as `count` from crawler_coin_abnormal_volume 
        where 1 = 1 '.$str.' 
        group by left(volume_at, 13)
    ');
}/*}}}*/

function get_crawler_abnormal_values()
{/*{{{*/
    return db_query('
        select *
        from crawler_coin_abnormal_volume
        order by at desc limit 1000
    ');
}/*}}}*/

function get_crawler_abnormal_values_by_symbol($symbol, $start_time, $end_time)
{/*{{{*/
    return db_query('
        select *
        from crawler_coin_abnormal_volume
        where symbol = :symbol
        and volume_at >= :start_time
        and volume_at < :end_time
        order by at desc
    ', [
        ':symbol' => $symbol,
        ':start_time' => $start_time,
        ':end_time' => $end_time
    ]);
}/*}}}*/

function get_crawler_abnormal_symbol_enum()
{/*{{{*/
    return db_query('select symbol, `key` from crawler_coin_abnormal_volume group by symbol');
}/*}}}*/
